package com.fengwk.user.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.fengwk.support.util.ResultUtils;
import com.fengwk.user.service.RandomService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

@Api(tags = "随机码")
@RestController
@RequestMapping("/api/random")
public class RandomController {

	@Autowired
	private HttpServletRequest request;
	@Autowired
	private HttpServletResponse response;
	
	@Autowired
	private RandomService randomService;

	@ApiOperation(
			value = "生成对应用户名的随机验证码,120秒超时", 
			response = String.class)
	@PostMapping("/usernameRandom")
	public void usernameRandom(
			@ApiParam(value = "用户名", required = true) @RequestParam(value = "username", required = true) String username) {
		ResultUtils.returnJsonResult(request, response, randomService.usernameRandom(username));
	}

	@ApiOperation(
			value = "生成对应手机号的随机验证码,发送至手机(未对接手机接口),120秒超时", 
			response = Void.class)
	@PostMapping("/mobilePhoneRandom")
	public void mobilePhoneRandom(
			@ApiParam(value = "手机号码", required = true) @RequestParam(value = "mobilePhone", required = true) String mobilePhone) {
		randomService.mobilePhoneRandom(mobilePhone);
		ResultUtils.returnJsonResult(request, response, true);
	}

	@ApiOperation(
			value = "生成对应邮箱的随机验证码,发送至邮箱,600秒超时", 
			response = Void.class)
	@PostMapping("/emailRandom")
	public void emailRandom(
			@ApiParam(value = "邮箱", required = true) @RequestParam(value = "email", required = true) String email) {
		randomService.emailRandom(email);
		ResultUtils.returnJsonResult(request, response, true);
	}
	
	@ApiOperation(
			value = "验证码校验", 
			response = boolean.class)
	@PostMapping("/checkRandom")
	public void checkRandom(
			@ApiParam(value = "身份", required = true) @RequestParam(value = "identity", required = true) String identity, 
			@ApiParam(value = "随机码", required = true) @RequestParam(value = "random", required = true) String random) {
		ResultUtils.returnJsonResult(request, response, randomService.checkRandom(identity, random));
	}
	
}
